﻿using System.Text;
using System.Linq;
using System.Collections.Generic;
using Eagles.Base.DataBase;
using Eagles.Application.Model.Enums;
using Eagles.DomainService.Model.User;
using Eagles.Interface.DataAccess.UserArticle;

namespace Ealges.DomianService.DataAccess.UserArticle
{
    public class ArticleDataAccess : IArticleDataAccess
    {
        private readonly IDbManager dbManager;

        public ArticleDataAccess(IDbManager dbManager)
        {
           this.dbManager = dbManager;
        }

        public int CreateArticle(TbUserNews userNews)
        {
            return dbManager.Excuted(@"insert into eagles.tb_user_news (OrgId,BranchId,NewsId,UserId,ToUser,Title,HtmlContent,NewsType,ImageUrl,Status,CreateTime,ViewsCount,RewardsScore,ReviewId,OrgReview,BranchReview,
IsPublic,PublicTime,BookName,BookAuthor,Attach1,Attach2,Attach3,Attach4,AttachName1,AttachName2,AttachName3,AttachName4)
values (@OrgId,@BranchId,@NewsId,@UserId,@ToUser,@Title,@HtmlContent,@NewsType,@ImageUrl,@Status,@CreateTime,@ViewsCount,@RewardsScore,@ReviewId,@OrgReview,@BranchReview,@IsPublic,@PublicTime,@BookName,@BookAuthor,
@Attach1,@Attach2,@Attach3,@Attach4,@AttachName1,@AttachName2,@AttachName3,@AttachName4) ", userNews);
        }

        public int EditArticle(TbUserNews userNews)
        {
            return dbManager.Excuted(@"update eagles.tb_user_news set NewsType=@NewsType,ToUser=@ToUser,Title=@Title,HtmlContent=@HtmlContent,ImageUrl=@ImageUrl,Status=@Status,OrgReview=@OrgReview,BranchReview=@BranchReview,
IsPublic=@IsPublic,PublicTime=@PublicTime,BookName=@BookName,BookAuthor=@BookAuthor,Attach1=@Attach1,Attach2=@Attach2,Attach3=@Attach3,Attach4=@Attach4,
AttachName1=@AttachName1,AttachName2=@AttachName2,AttachName3=@AttachName3,AttachName4=@AttachName4 where NewsId = @NewsId ", userNews);
        }

        public List<TbUserNews> GetUserNews(NewsEnum newsEnum, int userId, int pageIndex = 1, int pageSize = 10)
        {
            int pageIndexParameter = (pageIndex - 1) * pageSize;
            var userNews = dbManager.Query<TbUserNews>(@"select OrgId,BranchId,NewsId,UserId,ToUser,Title,HtmlContent,NewsType,ImageUrl,Status,CreateTime,ViewsCount,RewardsScore,ReviewId,IsPublic,PublicTime
OrgReview,BranchReview from eagles.tb_user_news where NewsType = @NewsType and UserId = @UserId order by CreateTime desc limit @PageIndex, @PageSize ",
new { NewsType = (int)newsEnum, UserId = userId, PageIndex = pageIndexParameter, PageSize = pageSize });
            return userNews;
        }

        public TbUserNews GetUserNewsDetail(int newsId, int appId)
        {
            var userNews = dbManager.Query<TbUserNews>(@"select a.OrgId,a.BranchId,a.NewsId,a.UserId,b.Name,a.ToUser,a.Title,c.Name as introducer, a.HtmlContent,a.NewsType,a.ImageUrl, 
a.Status,a.CreateTime,a.ViewsCount,a.RewardsScore,a.ReviewId,a.IsPublic,a.PublicTime ,a.OrgReview,a.BranchReview,ifnull(a.ViewCount,0) as ViewCount, a.BookName, a.BookAuthor, 
a.Attach1,a.Attach2,a.Attach3,a.Attach4,a.AttachName1,a.AttachName2,a.AttachName3,a.AttachName4, 
(select count(*) from eagles.tb_user_collect_log d where d.CollectType = 0 and Status = 0 and a.NewsId = d.NewsId) LikeCount,
(select count(*) from eagles.tb_user_collect_log d where d.CollectType = 1 and Status = 0 and a.NewsId = d.NewsId) FavoritesCount 
from eagles.tb_user_news a 
inner join eagles.tb_user_Info b on a.UserId=b.UserId left join eagles.tb_user_Info c on a.touser=c.UserId 
where a.NewsId = @NewsId and a.OrgId = @OrgId ", new { NewsId = newsId, OrgId = appId });
            if (userNews != null && userNews.Any())
                return userNews.FirstOrDefault();
            return null;
        }

        public List<TbUserNews> GetPblicUserNews(int appId, int branchId, bool isHome = false, int pageIndex = 1, int pageSize = 10)
        {
            int pageIndexParameter = (pageIndex - 1) * pageSize;                       
            var sql = new StringBuilder(@"select a.OrgId,a.BranchId,(select BranchName from eagles.tb_branch b where a.BranchId = b.BranchId) BranchName, a.NewsId,a.Title,a.HtmlContent,a.NewsType,a.UserId,
(select Name from eagles.tb_user_info c where a.UserId = c.UserId) Name,a.ToUser,a.ImageUrl,a.Status,a.CreateTime,a.ViewsCount,a.RewardsScore,a.ReviewId,a.IsPublic,a.PublicTime,
a.OrgReview,a.BranchReview from eagles.tb_user_news a where OrgId = @OrgId and Status = 0 ");
            //if (isHome)
            //{
            //    sql.Append("and ImageUrl is not null and ImageUrl <> '' ");
            //}
            if (branchId > 0)
            {
                //说明有用户
                sql.AppendFormat("and ((IsPublic = 3 and OrgReview = 0 ) or (IsPublic = 2 and BranchId = {0})) and BranchReview = 0 ", branchId);
            }
            else
            {
                sql.Append("and IsPublic = 3 and OrgReview = 0 and BranchReview = 0 ");
            }
            sql.Append("order by CreateTime desc limit @PageIndex, @PageSize ");
            return dbManager.Query<TbUserNews>(sql.ToString(), new { Orgid = appId, PageIndex = pageIndexParameter, PageSize = pageSize });
        }

        public TbUserNews GetPublicUserNewsDetail(int newsId, int appId)
        {
            var userNews = dbManager.Query<TbUserNews>(@"select OrgId,BranchId,NewsId,UserId,(select Name from eagles.tb_user_info b where a.UserId = b.UserId) Name,ToUser,Title,
HtmlContent,NewsType,ImageUrl,Status,CreateTime,ViewsCount,RewardsScore,ReviewId,IsPublic,PublicTime OrgReview,BranchReview,ifnull(ViewCount,0) as ViewCount,bookName,bookAuthor,
(select count(*) from eagles.tb_user_collect_log b where b.CollectType = 0 and Status = 0 and a.NewsId = b.NewsId) LikeCount,
(select count(*) from eagles.tb_user_collect_log b where b.CollectType = 1 and Status = 0 and a.NewsId = b.NewsId) FavoritesCount 
from eagles.tb_user_news a 
where NewsId = @NewsId and OrgId = @OrgId and IsPublic = 3 and OrgReview = 0 and BranchReview = 0 ", new { NewsId = newsId, OrgId = appId });
            if (userNews != null && userNews.Any())
                return userNews.FirstOrDefault();
            return null;
        }

        public TbUserNews GetUserRequisitionDetail(int appId, int userId)
        {
            var userNews = dbManager.Query<TbUserNews>(@"select a.OrgId,a.BranchId,a.NewsId,a.UserId,b.Name,a.ToUser,a.Title,c.Name as introducer, a.HtmlContent,a.NewsType,a.ImageUrl, 
a.Status,a.CreateTime,a.ViewsCount,a.RewardsScore,a.ReviewId,a.IsPublic,a.PublicTime ,a.OrgReview,a.BranchReview,ifnull(a.ViewCount,0) as ViewCount, a.BookName, a.BookAuthor, 
a.Attach1,a.Attach2,a.Attach3,a.Attach4,a.AttachName1,a.AttachName2,a.AttachName3,a.AttachName4 from eagles.tb_user_news a 
inner join eagles.tb_user_Info b on a.UserId=b.UserId left join eagles.tb_user_Info c on a.touser=c.UserId where a.UserId = @UserId and a.NewsType = 3 and a.OrgId = @OrgId 
and a.IsPublic = 1 order by a.NewsId desc", new { UserId = userId, OrgId = appId });
            if (userNews != null && userNews.Any())
                return userNews.FirstOrDefault();
            return null;
        }
    }
}